package Review;

class Solution2 {
    //定义遍历数组
    int[] dx = {-1,1,0,0,-1,-1,1,1};
    int[] dy = {0,0,-1,1,-1,1,-1,1};
    int m,n;
    public char[][] updateBoard(char[][] board, int[] click) {
        m = board.length;
        n = board[0].length;
        int x = click[0];
        int y = click[1];
        //直接踩到地雷,结束游戏
        if(board[x][y] == 'M'){
            board[x][y] = 'X';
            return board;
        }
        //走到这里说明没有直接踩到地雷
        dfs(board,x,y);
        return board;
    }
    void dfs(char[][] board,int i,int j){
        //先统计附近有几个地雷
        int count = 0;
        for(int k = 0;k<8;k++){
            int x = i + dx[k];
            int y = j + dy[k];
            if(x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'M'){
                count++;
            }
        }
        //周围没有地雷
        if(count == 0){
            board[i][j] = 'B';
            for(int k = 0;k<8;k++){
                int x = i + dx[k];
                int y = j + dy[k];
                if(x >=0 && x < m && y >=0 && y < n && board[x][y] == 'E'){
                    dfs(board,x,y);
                }
            }
            //周围没有地雷
        } else{
            board[i][j] = (char)('0' +count);
            return;
        }

    }
}